<template>
  <div class="app-container">
    <div class="filter-container">
      <org-unit-select v-model="filters.orgUnitId" placeholder="请选择店铺" :org-unit-type="1" />
      <room-select v-model="filters.roomId" placeholder="请选择房间" :org-unit-id="filters.orgUnitId"/>
      <el-date-picker
        v-model="selectedDate"
        type="daterange"
        align="right"
        unlink-panels
        range-separator="至"
        value-format="yyyy-MM-dd"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :picker-options="pickerOptions">
      </el-date-picker>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="handleFilter">查询</el-button>
      <el-button v-waves class="filter-item" type="primary" @click="handleExporter">导出</el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      border
      fit
      :height="tableHeight"
      :data="list"
      highlight-current-row
      style="width: 100%;"
    >
      <el-table-column prop="orgUnitName" label="门店" min-width="120" />
      <el-table-column prop="roomName" label="房间" width="120" />
      <el-table-column prop="operator" label="操作账号" width="120" />
      <el-table-column prop="movieName" label="剧本名称" width="120" />
      <el-table-column prop="startTime" label="开始时间" width="160" />
      <el-table-column prop="endTime" label="结束时间" width="160" />
      <el-table-column prop="typeStr" label="交易类型" width="100" />
      <el-table-column prop="amount" label="金额(元)" width="100" :formatter="formatAmount" />
      <el-table-column prop="luckyAmount" label="优惠扣减(元)" width="120" :formatter="formatLuckyAmount" />
      <el-table-column prop="payAmount" label="实付(元)" width="100" :formatter="formatPayAmount" />
      <el-table-column prop="accountRemained" label="交易完成时余额(元)" width="160" :formatter="formatAccountRemained" />
    </el-table>
  </div>
</template>

<script>
import { listConsumeDetails, downloadConsumeDetails } from '@/api/consume-details'
import OrgUnitSelect from '../components/org-unit-select'
import RoomSelect from '../components/room-select'
import waves from '@/directive/waves' // waves directive
import { mapGetters } from 'vuex'
import util from '@/utils/util.js'

export default {
  name: 'ConsumeDetails',
  components: { OrgUnitSelect, RoomSelect },
  directives: { waves },
  data() {
    return {
      tableHeight: 260,
      tableKey: 0,
      list: [],
      listLoading: true,
      sels: [], // 列表选中列
      filters: {
        orgUnitId: null,
        roomId: null
      },
      selectedDate: '',
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 6)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近30天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 29)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近90天',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 89)
            picker.$emit('pick', [start, end])
          }
        }]
      }
    }
  },
  computed: {
    ...mapGetters(['userInfo', 'formLabelWidth', 'dynamicColumn'])
  },
  created() {
    this.tableHeight = window.innerHeight - 160
    this.listLoading = false
  },
  methods: {
    formatAmount(row) {
      return util.currency.format(row.amount)
    },
    formatLuckyAmount(row) {
      return util.currency.format(row.luckyAmount)
    },
    formatPayAmount(row) {
      return util.currency.format(row.payAmount)
    },
    formatAccountRemained(row) {
      return util.currency.format(row.accountRemained)
    },
    loadPageList() {
      this.listLoading = true
      const param = { ...this.filters }
      if (this.selectedDate && this.selectedDate.length >= 2) {
        param.start = this.selectedDate[0]
        param.end = this.selectedDate[1]
      }
      listConsumeDetails(param).then(({ success, message, data }) => {
        this.listLoading = false
        if (success) {
          this.list = data
        } else {
          this.$message({
            message: message || '查询列表失败',
            type: 'error'
          })
        }
      }, () => {
        this.listLoading = false
      })
    },
    handleFilter() {
      this.filters.page = 1
      this.loadPageList()
    },
    handleExporter() {
      let fileName = '消费明细'
      this.$confirm('是否下载' + fileName, '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        const param = { ...this.filters }
        if (this.selectedDate && this.selectedDate.length >= 2) {
          param.start = this.selectedDate[0]
          param.end = this.selectedDate[1]
        }
        downloadConsumeDetails(param).then((res) => {
          this.listLoading = false
          // 获取heads中的filename文件名
          fileName = util.http.parseFilename(res) || fileName + '.xlsx'
          util.downloader.downloadBlob(res.data, fileName)
        }, (res) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
